<script setup>
import api from "./api.js";
import EditDialog from "./edit.vue";

const companyId = inject('companyId');

const tableData = ref([])

const tableRef = ref()
const editDialog = ref();

function handleAdd() {
  editDialog.value.open();
}

async function handleUpdate(row) {
  editDialog.value.open(row);
}

async function handleDelete(row) {
  await api.del(row.deptId)
  getList()
}

function getList() {
  if (!companyId.value) {
    return;
  }
  api.list(companyId.value).then(response => {
    tableData.value = response;
  }).catch(error => {
    console.log(error);
  });
}

watch(companyId, () => {
  getList();
})

</script>

<template>
  <div class="right-main" style="height: 100%;display: flex;flex-direction: column;">
    <div style="margin-bottom: 15px;">
      <el-button  @click="handleAdd">增加</el-button>
    </div>
    <vxe-table style="height: 100%;flex-grow: 1;"
        border="full"
        ref="tableRef"
        :column-config="{resizable: true}"
        :data="tableData">
      <vxe-column field="deptName" title="名称" tree-node></vxe-column>
      <vxe-column field="leader" title="负责人"></vxe-column>

      <vxe-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button style="margin-right: 10px;" link type="primary" size="small" @click="handleUpdate(row)">修改</el-button>
          <el-button size="small" link type="primary" @click="handleDelete(row)">删除</el-button>
        </template>
      </vxe-column>

    </vxe-table>

    <EditDialog ref="editDialog" @sub="getList"></EditDialog>

  </div>
</template>

<style scoped lang="scss">
  .right-main {
    flex-grow: 1;
    box-sizing: border-box;
    padding: 15px;
    margin-left: 130px;
    width: calc(100% - 130px);
  }
</style>